class Solution {
public:
    /**
     * @param A: The array A.
     * @return: The array of the squares.
     */
    vector<int> SquareArray(vector<int> &A) {
        int size = A.size();
        vector<int> res(size, 0);
        int first = 0;
        int last = size - 1;
        int idx = last;
        
        while (first <= last) {
            if (-A[first] < A[last]) {
                res[idx--] = A[last] * A[last];
                last--;
            } else {
                res[idx--] = A[first] * A[first];
                first++;
            }
        }
        
        return res;
    }
};
